﻿using System.Collections.Generic;
using System.Text;

namespace Zove.Compiler {
    public abstract class ZoomObject {

        public ZoomClass Parent {
            get;
            set;
        }

        public string Summary {
            get;
            set;
        }
    }

    public class ZoomFunction : ZoomObject {

        public string Name {
            get;
            set;
        }

        public string[] Parameters {
            get;
            set;
        }

        public string Body {
            get;
            set;
        }

        public string Description {
            get;
            set;
        }

        public int LineNumber {
            get;
            set;
        }

        public int Column {
            get;
            set;
        }
    }

    /// <summary>
    /// The variable
    /// </summary>
    public class ZoomVariable : ZoomObject {
        
        public string Type {
            get;
            set;
        }

        public string Name {
            get;
            set;
        }

        public string Unit {
            get;
            set;
        }
    }

    public class ZoomConstant : ZoomObject
    {

        public string Name
        {
            get;
            set;
        }

        public string Value
        {
            get;
            set;
        }
    }

    public class ZoomElement {
        public string Name {
            get;
            set;
        }

        public string Expression {
            get;
            set;
        }
    }

    public class ZoomEquation : ZoomObject {

        public int LineNumber {
            get;
            set;
        }

        public string Equation {
            get;
            set;
        }

        public string Left {
            get;
            set;
        }

        public string Right {
            get;
            set;
        }

        public ZoomEquation() {
            this.Elements = new List<ZoomElement>();
        }

        public List<ZoomElement> Elements {
            get;
            set;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class ZoomConstantCollection : List<ZoomConstant>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ZoomConstant GetConstant(string name)
        {
            foreach (ZoomConstant c in this)
            {
                if (c.Name == name)
                {
                    return c;
                }
            }

            return null;
        }
    }

    public class ZoomClass {

        public ZoomClass() {
            this.BaseClasses = new List<string>();
            this.Variables = new List<ZoomVariable>();
            this.Equations = new List<ZoomEquation>();
            this.Functions = new List<ZoomFunction>();
            this.Constants = new ZoomConstantCollection();
        }

        public string Name {
            get;
            set;
        }

        public string Url {
            get;
            set;
        }

        public List<string> BaseClasses {
            get;
            set;
        }

        public List<ZoomVariable> Variables {
            get;
            set;
        }

        public List<ZoomEquation> Equations {
            get;
            set;
        }

        public List<ZoomFunction> Functions {
            get;
            set;
        }

        public ZoomConstantCollection Constants
        {
            get;
            set;
        }
        
        public string Summary {
            get;
            set;
        }

        public override string ToString() {
            StringBuilder b = new StringBuilder();

            b.AppendLine("class " + this.Name);
            if (this.BaseClasses.Count > 0) {
                b.Append(" extends ");
                for(int i = 0; i < this.BaseClasses.Count; i++) {
                    b.Append(this.BaseClasses[i]);
                    if (i + 1 < this.BaseClasses.Count) {
                        b.Append(",");
                    }
                }
                b.AppendLine();
            }
            b.AppendLine("{");
            foreach (ZoomVariable var in this.Variables) {
                b.AppendFormat("\t{0} {1};\n", var.Type, var.Name);
            }

            b.AppendLine();
            foreach (ZoomEquation zeq in this.Equations) {
                b.AppendLine("\t" + zeq.Equation + ";");
            }
            b.AppendLine("}");

            return b.ToString();
        }
    }
}
